package samsung.sds.systemDictManagement.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;
import samsung.sds.systemDictManagement.model.SystemDictEntity;
import samsung.sds.systemDictManagement.service.SystemDictManagementService;
/**
 * @author tingcai.liu
 * @createDate 2016-02-03
 * 
 * 数据字典的Controller
 */
@Controller
@RequestMapping("/systemDictManagement")
public class SystemDictManagementController{

	@Autowired
	private SystemDictManagementService systemDictManagementServiceImpl;
	
	/**
	 * 跳转到数据字典列表界面
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/loadSystemDictManagement", method = RequestMethod.POST)
	public String loadSystemDictManagement(HttpServletRequest request, ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		return "/systemDictManagement/systemDictList";
	}
	
	/**
	 * 分页获取用户列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/loadSystemDictList", method = RequestMethod.POST)
	@ResponseBody
	public PageData<SystemDictEntity> loadSystemDictList(HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		return systemDictManagementServiceImpl.queryListPage(param);
	}
	
	/**
	 * 跳转到新增修改页面，并判断是新增还是修改
	 * @param request
	 * @param optionKey
	 * @param groupId
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/systemDictDetail", method = RequestMethod.POST)
	public String systemDictDetail(HttpServletRequest request,String optionKey,String groupId,ModelMap modelMap) {
		optionKey = request.getParameter("optionKey");
		groupId = request.getParameter("groupId");
		
		//根据option_key查询systemDict
		SystemDictEntity systemDict = new SystemDictEntity();
		systemDict.setOptionKey(optionKey);
		systemDict.setGroupId(groupId);
		systemDict = this.systemDictManagementServiceImpl.findUniqueByProperty(systemDict);
			
		//判断systemDict对象是否存在，判断是“新建”，还是“修改”
		if(systemDict == null) {
			//新建
			SystemDictEntity systemDictInformation = new SystemDictEntity();
			systemDictInformation.setOptionKey("");
			
			//如果选中记录，新建时的groupId即为选中的记录的groupId
			systemDictInformation.setGroupId(groupId);
			modelMap.addAttribute("systemDict", systemDictInformation);
		}else {
			modelMap.addAttribute("systemDict", systemDict);
		}
		
		return "/systemDictManagement/addOrUpdateSystemDict";
	}
	
	/**
	 * 新增或修改数据字典对象
	 * @param request
	 * @param systemDict
	 * @return
	 */
	@RequestMapping(value = "/addOrUpdateSystemDict", method = RequestMethod.POST)
	@ResponseBody
	public String addOrUpdateSystemDict(HttpServletRequest request, SystemDictEntity systemDict) {
		String optype = request.getParameter("optype");
		
		int isSuccess = systemDictManagementServiceImpl.addOrUpdateSystemDict(optype, systemDict);
		
		if(isSuccess == 1) {
			return Constants.SUCCESS_CODE;
		}else{
			return Constants.FAILURE_CODE;
		}
	}
	
	/**
	 * 删除项
	 * @param systemDict
	 * @return
	 */
	@RequestMapping(value = "/deleteSystemDict", method = RequestMethod.POST)
	@ResponseBody
	public String deleteSystemDict(SystemDictEntity systemDict) {
		int rlt = systemDictManagementServiceImpl.deleteSystemDict(systemDict);
		
		if(rlt==1){
			return Constants.SUCCESS_CODE;
		}else{
			return Constants.FAILURE_CODE;
		}
	}
	
	/**
	 * 删除组
	 * @param systemDict
	 * @return
	 */
	@RequestMapping(value = "/deleteSystemDictByGroupId", method = RequestMethod.POST)
	@ResponseBody
	public String deleteSystemDictByGroupId(SystemDictEntity systemDict) {
		
		int isSuccess = systemDictManagementServiceImpl.deleteSystemDictByGroupId(systemDict);
		
		if(isSuccess == 1){
			return Constants.SUCCESS_CODE;
		}else{
			return Constants.FAILURE_CODE;
		}
	}
	
	/**
	 * 检查optionKey是否重复
	 * @param parammeters
	 * @return
	 */
	@RequestMapping(value = "/checkOptionKeyRepeat", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkOptionKeyRepeat(String parammeters) {
		return systemDictManagementServiceImpl.checkOptionKeyRepeat(parammeters);
	}
	
	/**
	 * 检查groupId是否已经存在是否重复
	 * @param systemDict
	 * @return
	 */
	@RequestMapping(value = "/checkGroupIdRepeat", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkGroupIdRepeat(SystemDictEntity systemDict) {
		return systemDictManagementServiceImpl.checkGroupIdRepeat(systemDict);
	}
}
